package com.jl.smartcard.core;

import java.io.File;
import java.io.IOException;
import java.net.URL;

import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.viewers.TreeViewer;


public class ProjectConfig {

	public final static String FOLDER_JS = "JavaScript";
	public final static String FOLDER_DEFAULT = "DEFAULT";

	private static TreeViewer viewer = null;

	public static void setTreeViewer(TreeViewer tree) {
		viewer = tree;
	}

	public static File getDEFAULT() {
		File f = getWorkspace();
		String path = f.getPath();
		path = path + File.separator + "DEFAULT";
		File file = new File(path);
		if (!file.exists()) {
			file.mkdir();
		}
		return file;
	}

	public static File getDEFAULTLog(String name) {
		File de = getDEFAULT();
		String path = de.getAbsolutePath();
		path = path + File.separator + "log";
		File logFolder = new File(path);
		if (!logFolder.exists()) {
			logFolder.mkdir();
		}
		path = path + File.separator + name;
		File logFile = new File(path);
		if (!logFile.exists()) {
			try {
				logFile.createNewFile();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return logFile;
	}

	public static File getDEFAULTJs(String name) {
		File de = getDEFAULT();
		String path = de.getAbsolutePath();
		path = path + File.separator + FOLDER_JS;
		File logFolder = new File(path);
		if (!logFolder.exists()) {
			logFolder.mkdir();
		}
		path = path + File.separator + name;
		File logFile = new File(path);
		if (!logFile.exists()) {
			try {
				logFile.createNewFile();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return logFile;
	}

	public static File getWorkspace() {
		// /String path = System.getProperty("user.dir");
		URL uri = null;
		uri = Platform.getInstanceLocation().getURL();
		String t = uri.getFile();
		return new File(t);
	}

	public static boolean isLogFile(File f) {
		String name = f.getName();
		String parent = f.getParent();
		if (!parent.toLowerCase().endsWith("log")) {
			return false;
		}
		if (!name.toLowerCase().endsWith(".log")) {
			return false;
		}
		return true;
	}

	public static File getCurrentLog(String name) {
		return null;
	}

	public static File getCurrentJs(String name) {
		return null;
	}

	public static boolean isJsFile(File file) {
		if (!file.isFile()) {
			return false;
		}
		String name = file.getName();
		return name.toLowerCase().endsWith("js");
	}

}
